package Utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class ConnectionUtil {
    private static String url = "jdbc:mysql://localhost:3306/hrms?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";
    private static String rootName = "root";
    private static String rootPwd = "123456";

    private ConnectionUtil() {
    }

    static {
        try {
            /*加载驱动*/
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * getConnection:创建连接.<br/>
     *
     * @return
     * @throws SQLException
     */
    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(url, rootName, rootPwd);
    }

    /**
     * closeRe:关闭连接.<br/>
     *
     * @param con
     * @param pt
     * @param rs
     */
    public static void closeRe(Connection con, PreparedStatement pt, ResultSet rs) {
        closeQuietly(rs);
        closeQuietly(pt);
        closeQuietly(con);
    }

    /**
     * closeRe:关闭连接.<br/>
     *
     * @param con
     * @param pt
     */
    public static void closeRe(Connection con, PreparedStatement pt) {
        closeQuietly(pt);
        closeQuietly(con);
    }

    /**
     * closeQuietly:通用关闭方法.<br/>
     *
     * @param ac
     */
    private static void closeQuietly(AutoCloseable ac) {
        if (ac != null) {
            try {
                ac.close();
            } catch (Exception ignored) {
            }
        }
    }

}